const Koa = require('koa')
    , bodyParser = require('koa-bodyparser')
    , koaLogger = require('koa-logger')
    , app = new Koa()
    , routers = require('./modules/routers')

// 配置控制台日志中间件
 // app.use((koaLogger()))

// 配置ctx.body解析中间件
app.use(bodyParser())

// sql注入
app.use(SQLInjectionAttack)

// 初始化路由中间件
app.use(routers.routes()).use(routers.allowedMethods())

// 监听启动端口
app.listen( 3001 , 'localhost')
console.log(`the server is start at port ${3001}`)

async function SQLInjectionAttack(ctx, next) {
  for (let k in ctx.request.body)
      ctx.request.body[k] = ctx.request.body[k].replace(/'|"/g, '')
  return next()
}
